package com.jy.api.controller.common;

import com.jy.api.dao.*;
import com.jy.api.domain.*;
import com.jy.api.service.OpenSearchService;
import com.jy.api.service.ProductService;
import com.jy.api.signUpReq.CloneProductSignUpReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lihu on 2017/12/20.
 */

@Slf4j
@RequestMapping(value = "/v1/clone", produces = "application/json;charset=utf-8")
@RestController
public class ProductCloneCotroller {


    @Autowired
    ProductService productService;

    @Autowired
    ProductDao productDao;
    @Autowired
    private ProductTypeDao productTypeDao;
    @Autowired
    OpenSearchService openSearchService;
    @Autowired
    private ProductMaterialDao productMaterialDao;
    @Autowired
    private MaterialUnitDao materialUnitDao;

    /**
     * 店铺商品克隆
     *
     * @param
     * @return
     */
    @GetMapping(value = "/product")
    public ResponseEntity cloneOrduct(@RequestParam("storeId") String storeId, @RequestParam("parentStoreId") String parentStoreId) {
        try {
            CloneProductSignUpReq cloneProductSignUpReq = new CloneProductSignUpReq(parentStoreId, storeId);
            Product product = productDao.queryProduct(storeId);
            if (product != null) {
                return new ResponseEntity("克隆失败！店铺已存在商品信息！", HttpStatus.BAD_REQUEST);
            }
            productService.cloneProduct(cloneProductSignUpReq);
            return new ResponseEntity(HttpStatus.OK);
        } catch (Exception ex) {
            log.error("sendNotice is error:{}", ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }


    /**
     * 店铺商品克隆
     *
     * @param
     * @return
     */
    @GetMapping(value = "/mterial")
    public ResponseEntity material(@RequestParam("storeId") String storeId, @RequestParam("parentStoreId") String parentStoreId) {
        try {
            List<ProductMaterial> product = productMaterialDao.findByStoreId(storeId);
            if (product.size() > 0) {
                return new ResponseEntity("克隆失败！店铺已存在商品原料信息！", HttpStatus.BAD_REQUEST);
            }
            //两个父级
            List<MaterialUnit> materialUnits = materialUnitDao.findByStoreIdOrderByIdDesc(parentStoreId);
            List<ProductMaterial> parentId = productMaterialDao.findByStoreId(parentStoreId);
            HashMap map = new HashMap();
            for (MaterialUnit materialUnit : materialUnits) {
                MaterialUnit unit = new MaterialUnit();
                unit.setName(materialUnit.getName());
                unit.setStoreId(storeId);
                MaterialUnit save = materialUnitDao.save(unit);
                map.put(materialUnit.getId(), save.getId());
                log.info("查询出来的map{}",map);
            }
            for (ProductMaterial productMaterial : parentId) {
                ProductMaterial material = new ProductMaterial();
                material.setName(productMaterial.getName());
                material.setStoreId(storeId);
                material.setUnitId(Integer.parseInt(map.get(productMaterial.getUnitId()).toString()));
                material.setPy(productMaterial.getPy());
                productMaterialDao.save(material);
            }
            return new ResponseEntity(HttpStatus.OK);
        } catch (Exception ex) {
            log.error("sendNotice is error:{}", ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }


    @GetMapping(value = "update-net")
    public ResponseEntity updateNet(@RequestParam("storeId") String storeId) {
        List<Product> product = productDao.findByStoreIdAndTypeName(storeId, "火星网费");
        List<Product> list = new ArrayList<>();
        ProductType type = productTypeDao.findOne("1");
        for (Product p : product) {
            p.setProductType(type);
            // openSearchService.openSearchProductPush(p);
            list.add(p);
        }
        productDao.save(list);
        return ResponseEntity.ok("克隆成功");
    }

    @GetMapping("delete-product")
    public ResponseEntity deleteProduct(@RequestParam("storeId") String storeId) {
        List<Product> products = productDao.findByStoreId(storeId);
        productDao.delete(products);
        return ResponseEntity.ok("删除成功");
    }


}
